package com.itlong.cloud.intercept;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.enumerate.ecode.MiniProgramErrorCodeEnum;
import com.itlong.cloud.user.app.IAppMineServiceClient;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *     小程序项目状态拦截器
 * </desc>
 * @createDate 2019/01/07
 */
@Aspect
@Component
public class ProjectStatusIntercept {
    private static final Logger LOG = LoggerFactory.getLogger(ProjectStatusIntercept.class);

 // 定义一个切入点
    @Pointcut("execution(* com.itlong.cloud.controller.miniprogram..*.*(..))")
    public void excudeService(){}

    @Autowired
    IAppMineServiceClient iAppMineServiceClient;

    /**
     * <desc>
     *     项目状态拦截器
     * </desc>
     * @param thisJoinPoint
     * @return
     * @throws Throwable
     * @author Qiang.S
     * @createDate 2019/01/07
     */
    @Around("excudeService()")
    public Object twiceAsOld(ProceedingJoinPoint thisJoinPoint) throws Throwable{
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String projectId=request.getParameter("projectId");//获取项目编号
        if (projectId == null) {
            projectId = request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();
        }
        if(StringUtils.isNotBlank(projectId)){
            String status = iAppMineServiceClient.getProjectStatus(projectId);
            if (status != null && "1".equals(status)) {
                return thisJoinPoint.proceed();
            }
            return new MessageVO(MiniProgramErrorCodeEnum.PROJECT_IS_BANNED.getErrorCode());
        }else{
            return thisJoinPoint.proceed();
        }
    }
}
